//
//  PracticeVC.m
//  doubleEduStudents
//
//  Created by 盛洋 on 2021/1/20.
//  Copyright © 2021 shengYang. All rights reserved.
//

#import "PracticeVC.h"
#import "practiceCell.h"

#define  classCount  5

@interface PracticeVC ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (strong, nonatomic) IBOutlet UIView *count_view;
@property (strong, nonatomic) IBOutlet UILabel *count_lab;
@property (strong, nonatomic) IBOutlet UIButton *up_btn;
@property (strong, nonatomic) IBOutlet UIButton *next_btn;
@property (strong, nonatomic) IBOutlet UICollectionView *collection_view;

@end

@implementation PracticeVC{
    int count;//页码
    NSArray *countArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //底部加减按钮
    [self setBottomView];
    [self.collection_view registerNib:[UINib nibWithNibName:@"practiceCell" bundle:nil] forCellWithReuseIdentifier:@"practiceCell"];
    self.collection_view.delegate = self;
    self.collection_view.dataSource = self;
    countArr = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
}

#pragma mark ----- collectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return countArr.count;
}

//每个item的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(self.collection_view.frame.size.width,self.collection_view.frame.size.height);
}

//列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
//行间距（同一行的cell的间距）
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

//cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    practiceCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"practiceCell" forIndexPath:indexPath];
    return cell;
}


//底部加减按钮
-(void)setBottomView{
    APPViewBorderRadius(self.count_view, SCREEN_H*0.15/2, 1, [UIColor blackColor]);
    [self.next_btn setHidden:YES];
    [self.up_btn setTitle:@"下一题" forState:UIControlStateNormal];
    count = 1;
    self.count_lab.text = [NSString stringWithFormat:@"%d/%d",count,classCount];
    self.count_lab.font = AutoLabFont2;
    self.next_btn.titleLabel.font = self.up_btn.titleLabel.font = AutoLabFont2;
}

//上一题按钮
- (IBAction)upBtn:(id)sender {
    if ([self.up_btn.titleLabel.text isEqualToString:@"下一题"]) {
        count ++;
        self.count_lab.text = [NSString stringWithFormat:@"%d/%d",count++,classCount];
        [self.collection_view scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:count - 2 inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    }
    
    [self.next_btn setHidden:NO];
    [sender setTitle:@"上一题" forState:UIControlStateNormal];
    if (count <= classCount && count >1) {
        count --;
        self.count_lab.text = [NSString stringWithFormat:@"%d/%d",count,classCount];
        
        [self.collection_view scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:count - 1 inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    }
    
}

//下一题按钮
- (IBAction)next_btn:(id)sender {
    count ++;
    if (count > classCount) {
        count = classCount;
    }else{
        self.count_lab.text = [NSString stringWithFormat:@"%d/%d",count,classCount];
    }
    [self.collection_view scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:count - 1 inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    int cuttentIndex = (int)(scrollView.contentOffset.x + self.collection_view.frame.size.width/2)/self.collection_view.frame.size.width;
    self.count_lab.text = [NSString stringWithFormat:@"%d/%d",cuttentIndex+1,classCount];
    if (cuttentIndex+1 >= 2) {
        [self.next_btn setHidden:NO];
        count = cuttentIndex + 1;
        [self.up_btn setTitle:@"上一题" forState:UIControlStateNormal];
    }else{
        if (cuttentIndex == 0) {
            count = 1;
        }else{
        count = cuttentIndex;
        }
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
